<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建订单配置':'编辑订单配置'" :close-on-click-modal="true" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="orderForm" :model="form" label-width="180px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="秒杀订单超时关闭时间(分)" prop="flashOrderOvertime">
          <el-input-number v-model="form.flashOrderOvertime" :min="0" :precision="0" :step="1" />
        </el-form-item>
        <el-form-item label="正常订单超时时间(分)" prop="normalOrderOvertime">
          <el-input-number v-model="form.normalOrderOvertime" :min="0" :precision="0" :step="1" />
        </el-form-item>
        <el-form-item label="自动确认收货时间(天)" prop="confirmOvertime">
          <el-input-number v-model="form.confirmOvertime" :min="0" :precision="0" :step="1" />
        </el-form-item>
        <el-form-item label="自动完成交易时间(天)" prop="finishOvertime">
          <el-input-number v-model="form.finishOvertime" :min="0" :precision="0" :step="1" />
        </el-form-item>
        <el-form-item label="自动好评时间(天)" prop="commentOvertime">
          <el-input-number v-model="form.commentOvertime" :min="0" :precision="0" :step="1" />
        </el-form-item>
        <el-form-item label="所需会员成长值" prop="memberGrowth">
          <el-select v-model="form.memberGrowth">
            <el-option
              v-for="item in optionOfMemberGrowth"
              :key="item.id"
              :label="item.name"
              :value="item.growthPoint"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { memberLevelList } from '@/api/system/member'
import { orderSettingsLoad, orderSettingsSave } from '@/api/mall/order/settings'

export default {
  name: 'OrderSettingDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        flashOrderOvertime: undefined,
        normalOrderOvertime: undefined,
        confirmOvertime: undefined,
        finishOvertime: undefined,
        commentOvertime: undefined,
        memberGrowth: 0
      },
      optionOfMemberGrowth: []
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
        this.getDictData()
      })
    },
    loadForm(id) {
      orderSettingsLoad({id: id}).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['mall:order:settings'])
      }).catch(() => {})
    },
    getDictData() {
      memberLevelList().then(response => {
        this.optionOfMemberGrowth = response
      })
    },
    onSubmit() {
      this.$refs.orderForm.validate(valid => {
        if (valid) {
          orderSettingsSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.$refs.orderForm.resetFields()
      this.visible = false
    }
  }
}
</script>
